#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "unistd.h"
#include "string.h"

#define LED_TRIGGER     "/sys/class/leds/sys-led/trigger"
#define LED_BRIGHTNESS  "/sys/class/leds/sys-led/brightness"

#define USAGE()   fprintf(stderr, "usage:\n"  \
                        "   %s<on|off>\n"   \
                        "   %s<trigger><type>\n", argv[0],argv[0])


int main(int argc, char *argv[])
{
    int fd_trigger, fd_brightness;

    /* 判断输入参数个数 */
    if( argc < 2 ) {
        USAGE();
        exit(-1);
    }

    /* 打开文件 */
    fd_trigger = open(LED_TRIGGER, O_RDWR);
    if (fd_trigger < 0 ) {
        perror("open trigger error");
        exit(-1);
    }

    fd_brightness = open(LED_BRIGHTNESS, O_RDWR);
    if (fd_brightness < 0 ) {
        perror("open brightness error");
        exit(-1);
    }

    /* 判断输入参数 */
    if ( !strcmp(argv[1],"on") ) {
        write(fd_trigger, "none", 4);
        write(fd_brightness, "1", 1);
    } else if ( !strcmp(argv[1], "off") ) {
        write(fd_trigger, "none", 4);
        write(fd_brightness, "0", 1);
    } else if ( !strcmp(argv[1], "trigger")) {
        if ( argc != 3 ) {
            USAGE();
            exit(-1);
        }

        if ( write(fd_trigger, argv[2], strlen(argv[2]) ) < 0 ) {
            perror("set trigger faile");
        }
    } else {
        USAGE();
    }

    close(fd_trigger);
    close(fd_brightness);

    exit(0);
}
